<?php

class m_rights_actions_AjaxListRightsAction extends m_backoffice_extends_Action
{
    private $group;
    private $isSuGroup;

	public function execute()
	{
        $root = $this->getRequestParameter('root');
        $docLang = $this->getRequestParameter('docLang');
        $groupId = $this->getRequestParameter("docId");
		$subTabName = $this->getRequestParameter("subTabName", "website");
        $accordionTabName = $this->getRequestParameter("accordionTabName");

        if(!is_null($groupId))
        {
            $this->group = $this->getDocumentProvider()->getByUniqueId($groupId);
            $this->isSuGroup = m_rights_services_RightsService::getInstance()->isInheritSuGroup($this->group);
        }

        $view = $this->getDocList($subTabName, $accordionTabName, $root, $docLang);
        echo json_encode($view);
	}

	public function validate()
	{
		return $this->getUserContext()->hasRight(array("backoffice_accessBo","rights_Show"));
	}

    private function getDocList($subTabName, $accordionTabName = null, $root, $docLang)
    {
        $rootNodeService = m_backoffice_services_RootNodeService::getInstance();
        $listService = m_backoffice_services_ListService::getInstance();
        $documents = array();

        if($root == "source")
        {
            if($subTabName == 'modules' && empty($accordionTabName))
            {
                return null;
            }
            else
            {
                $rootId = empty($accordionTabName) ? $rootNodeService->getModuleRootNode($subTabName) : $rootNodeService->getModuleRootNode($accordionTabName);
                $rootId = $rootId->getUniqueId();
            }
        }
        else
        {
            $rootId = $root;
        }

        switch($subTabName)
        {
            default:
            break;
            case "website":
                $documents = $listService->getModuleListedDocuments($subTabName);
            break;
            case "medias":
                $documents = $listService->getModuleListedDocuments($subTabName);
            break;
            case "modules":
                if(!empty($accordionTabName))
                {
                    $documents = $listService->getModuleListedDocuments($accordionTabName);
                    $docList = $this->getMenuItems($rootId, $accordionTabName, $documents, $docLang);

                    return $docList;
                }
            break;
        }

        if(!empty($documents))
        {
            $docList = $this->getMenuItems($rootId, $subTabName, $documents, $docLang);
        }

        return $docList;
    }

	private function getMenuItems($nodeId, $module, $documentModel, $docLang = "*")
	{
        $document = $this->getDocumentProvider()->getByUniqueId($nodeId);
		$documents = f_relation_ManagerNew::getInstance($document)->getChilds()->withModel($documentModel)->withLang($docLang)->retrieveDocuments();
        $tabValues = array();

		$nbDocuments = count($documents);

		if($nbDocuments == 0)
        {
            return $tabValues;
        }

		foreach($documents as $key => $document)
		{
            $tabValue = array();
            $result = "";
            
            if(method_exists($document, 'getIcon'))
            {
                $icon = $document->getIcon();
                $iconPath = preg_match("#(http://)#", $icon) ? $icon : $this->getIcon($icon);
            }
            else
            {
                $icon = count($document->relationFields()) > 0 ? "folder.png" : "page_white.png";
                $iconPath = $this->getIcon($icon);
            }

            $result .= '<img src="'.$iconPath.'" class="arbo">'.$document->getLabel();
            $result .= $this->getRightsIcon($module, $document);

            $tabValue['text'] = $result;

            if($document->getDocumentModel() == "modules_website/page")
            {
                $tabValue['children'] = $this->getBlockList($document);
            }
            else
            {
                $tabValue['id'] = $document->getUniqueId();
                $tabValue['docLang'] = $document->getLang();

                $tabValue['hasChildren'] = f_relation_ManagerNew::getInstance($document)->getChilds()->withModel($documentModel)->withLang($document->getLang())->count() > 0 ? true : false;
            }

            $tabValues[] = $tabValue;
		}

		return $tabValues;
	}

    private function getBlockList($document)
    {
        $blocks = $document->getAllBlocks();
        $tabValues = array();

        if(!empty($blocks))
        {
            foreach($blocks as $block)
            {
                $result = "";
                $result .= '<span><img src="'.$this->getIcon("block.png").'" class="arbo" />'.$block['blockLabel'].'</span>';
                $result .= $this->getRightsIcon('page', $document, $block);
                
                $tabValues[]['text'] = $result;
            }
        }

        return $tabValues;
    }

    private function getRightsIcon($module, $document, $block = null)
    {
        $activeIcon = 'active_right.png';
        $unactiveIcon = 'unactive_right.png';
        $activeIcon2 = 'active_right2.png';
        $lockIcon = 'lockedblock.png';
        $lockIcon2 = 'lockedblock.png';

        if(!is_null($block))
        {
            $rights = $document->getBlockRight($this->group->getUniqueId(), $block["blockId"]);
        }
        else
        {
            $rights = m_rights_services_RightsService::getInstance()->getRightsFromDocument($this->group->getUniqueId(), $module, $document);
        }

        $result = "";
        $rights = array_reverse($rights);
        foreach($rights as $key => $right)
        {
            $js = "";
            $icon = $activeIcon;

            switch($right)
            {
                case '0':
                    $icon = $unactiveIcon;
                break;
                case '1':
                    $icon = $activeIcon;
                break;
                case '2':
                    $icon = $activeIcon2;
                break;
                case '3':
                    $icon = $lockIcon;
                break;
                case '4':
                    $icon = $lockIcon2;
                break;
            }

            if(!$this->isSuGroup && $right != '2' && $right != '4')
            {
                $js .= 'onclick="changeDocRights('.$this->group->getUniqueId().', \''.$module.'\', '.$document->getUniqueId().', \''.$document->getLang().'\', \''.$key.'\'';
                if(!is_null($block))
                {
                    $js .= ', ' . $block['blockId'];
                }
                $js .= ')"';
                $js .= ' style="cursor: pointer;"';
            }

            $blockId = !is_null($block) ? '_'.$block['blockId'] : '';

            $result .= '<div id="'.$document->getUniqueId().'_'.$document->getLang().'_'.$module.'_'.$key.$blockId.'" class="listRights">';
            $result .= '<img src="'.$this->getIcon($icon).'" '.$js.' />';
            $result .= "</div>";
        }
        return $result;
    }

    private function getIcon($icon)
    {
        return URL_RELATIVE.'/themes/'.BACK_THEME.'/backend/medias/icons/1616/'.$icon;
    }
}